package com.chb.test.useThreadsafe_06;

public class MyThread extends Thread{
    static int ticket = 0;

    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }
    // 锁对象，一定要唯一的
    static Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.class) { // 锁对象唯一
                if(ticket < 100) {
                    try {
                        Thread.sleep(50);
                        ticket++;
                        System.out.println(getName() + "正在卖第" + ticket + "张票");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }
    }
}
